/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.0 Professional
Automatic Program Generator
 Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : 
Version : 
Date    : 19.11.2011
Author  : 
Company : 
Comments: 


Chip type               : ATmega8
Program type            : Application
AVR Core Clock frequency: 8,000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 256
*****************************************************/

#include <mega8.h>
#asm
    .equ __w1_port=0x18
    .equ __w1_bit=3  
    .equ __lcd_port=0x18
#endasm
//#include <1wire.h>
#include "ds18x20_v2.h"
#include <delay.h>
#include <bcd.h>
#include <lcd.h>
#include <stdio.h>

//---------------------------------------
char lcd_buffer[33];

static flash unsigned char digits[] = {
    0b00000110,       // 1
    0b01011011,   // 2
    0b01001111,   // 3
    0b01100110,     // 4
    0b01101101,     // 5
    0b01111101,   // 6
    0b00000111,       // 7
    0b01111111, // 8
    0b01101111,   // 9
    0b00111111,   // 0 
    0b01110001,       // F - 15
    0b01000000,         // 16 -  
    0b00000000,          // 18 - 
    0b10000000          // 18 - 
};
#define POINT = 13;
#define F = 10;
#define MINUS = 11;
#define PROBEL = 12;

//1,2,3
static flash unsigned char cursor[]={0b11111110, 0b11111101, 0b11111011};
unsigned char digit_out[3]={10,12,0}, cur_dig=0;
bit zpt_ON;
int temperature;       // ,    
#define MAX_DS18x20 4
unsigned char rom_code[MAX_DS18x20][9];

// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void){
    //TCNT0=0xE6;// Reinitialize Timer 0 value 
    
    //       7  
    //          
    //   Onn -  ,     
    //   

    PORTC|=0b11111111;    //   (   "1" -  ..)
        
    PORTD=digits[digit_out[cur_dig]]; //   

    if ((zpt_ON == 1) && (cur_dig == 1)) PORTD.7=1;  //     ( .  )        

    PORTC&=(cursor[cur_dig]);  //    (   "0" -  ..) 

    cur_dig++; if (cur_dig > 2) cur_dig=0;





}


 //----------------------------------------------------------------------------
void view_term(void) {
    unsigned char celie, drob, tmp;
    unsigned int temp, celie_tmp, drob_tmp;


    temp = (unsigned int) temperature;
        
    if (temperature>=0) {
        digit_out[0]=PROBEL;       //   ()  
    } else {        
        temp = ( ~temp ) + 0x0001; //       . 
        digit_out[0]=MINUS;        //     (   ..)
         };
    //digit_out[3]=GRADUS;
    zpt_ON=1; 

    celie_tmp = temp >> 4;              //   
    drob_tmp  = temp & 0x000F;          //      1/16 

    drob  = (unsigned char) ((drob_tmp * 10) / 16); //      "1/16 "      0,1  

    celie = (unsigned char) celie_tmp;  //     
//
//    if (celie >= 100) {tmp=bin2bcd(celie-100); digit_out[0] = 1; } else { tmp=bin2bcd(celie); }   //   
//
//    if ((celie >= 100) || ((digit_out[0] == MINUS) && (celie >= 10))) { //    100,0 .   .  -10  1 .
//        zpt_ON=0;
//        digit_out[1] = tmp >> 4;    // 
//        digit_out[2] = tmp & 0x0F;  //  
//    }

//    if ((digit_out[0] == MINUS) && (celie < 10)) { //  . 0   -10  0,1 .
//        digit_out[1] = celie; // 
//        digit_out[2] = drob;  //  
//    }
//
//    if ((digit_out[0] == PROBEL) && (celie < 100)) { //  .  100,0    0   0,1 .
//        if (tmp >> 4) digit_out[0] = tmp >> 4; //  -       
//        digit_out[1] = tmp & 0x0F;             // 
//        digit_out[2] = drob;                   //  
//    }
}  // -------------------------------------------------------------------------



// Declare your global variables here

void main(void)
{
////// Declare your local variables here
unsigned char i, ds18x20_devices;
//char *_lcd_buffer="Hello.";

// Input/Output Ports initialization
// Port B initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=In Func2=Out Func1=Out Func0=Out 
// State7=0 State6=0 State5=0 State4=0 State3=P State2=0 State1=0 State0=0 
PORTB=0x08;
DDRB=0xF7;

// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=Out Func1=Out Func0=Out 
// State6=P State5=P State4=P State3=P State2=1 State1=1 State0=1 
PORTC=0x7F;
DDRC=0x07;

// Port D initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out 
// State7=1 State6=1 State5=1 State4=1 State3=1 State2=1 State1=1 State0=1 
PORTD=0xFF;
DDRD=0xFF;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 1000,000 kHz
TCCR0=0x02;
TCNT0=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;

// Analog Comparator initialization
// Analog Comparator: Off
ACSR=0x80;

////// 1 Wire Bus initialization
w1_init();

//// Determine the number of DS18x20 devices connected to the 1 Wire bus
ds18x20_devices=w1_search(0xf0,rom_code);

lcd_init(16);

// Global enable interrupts
#asm("sei")


sprintf(lcd_buffer,"      Hello.           Hi       ");
lcd_clear();
lcd_puts(lcd_buffer);

////    F '-  '
//digit_out[0]=0b01110001;
//digit_out[1]=0b00000000;
digit_out[2]=ds18x20_devices;

// ..       
//      
if (ds18x20_devices >= 1) {
    for (i=0;i<ds18x20_devices;i++) {
//        if (rom_code[i][0] == DS18B20_FAMILY_CODE){
//            temperature=ds18b20_temperature(&rom_code[i][0]); 
//        }
        if (rom_code[i][0] == DS18S20_FAMILY_CODE){
            temperature=ds18s20_temperature(&rom_code[i][0]); 
        }
        delay_ms(1000);
    }
} else {
    delay_ms(2000);
}




while (1)
      { 
          
        sprintf(lcd_buffer,"      Hello.           Hi       ");
        lcd_clear();
        lcd_puts(lcd_buffer);
//        lcd_gotoxy(0,0);
//        lcd_puts(_lcd_buffer);
        delay_ms(100);    
//        digit_out[2]=ds18x20_devices;
        if (ds18x20_devices >= 1) {  //     

                for (i=0;i<ds18x20_devices;) {
                        if (rom_code[i][0] == DS18B20_FAMILY_CODE){
                             temperature=ds18b20_temperature(&rom_code[i][0]); 
                        }
                        if (rom_code[i][0] == DS18S20_FAMILY_CODE){
                             temperature=ds18s20_temperature(&rom_code[i][0]); 
                        }
                        if (temperature!=-9999) view_term();

                        delay_ms(2000);
                        i++;
                }
        }

      }
}
